require(["common", "jQuery", "coAjax", "queryString", "hash_routie"], function(jSouper, $, coAjax, QueryString, hash_routie) {
    var _num = 10;
    var _page = 0;
    var _type;
    var typeMap = {
        "3": "全部",
        "1": "未付款",
        "2": "未发货"
    };
    var typeNumMap = {};
    for (var k in typeMap) {
        typeNumMap[typeMap[k]] = k;
    }

    function _load_achs_page(type, num, page) {
        //状态标识
        if (type === "未付款") {
            App.set("$Cache.bus.achs.type", "待付款订单");
        } else if (type === "未发货") {
            App.set("$Cache.bus.achs.type", "待发货订单");
        } else {
            App.set("$Cache.bus.achs.type", "所有订单");
        }
        coAjax.get(appConfig.bus.achs_with_type_and_page_info, {
            type: type,
            num: num,
            page: page
        }, function(result) {
            var info = result.result;
            // App.asyncArraySet("bus.achs_detail",info.achs)
            App.set("bus.achs_detail", info.achs);
            App.set("bus.achs_bak", info.achs); //数据备份
            page_info = {
                total_num: info.total_num,
                total_page: info.total_page,
                page: info.page,
                num: info.num
            };
            page_info._number_list = [];
            page_info._number_list.length = info.total_page;
            App.set("$Cache.bus.achs.page_info", page_info);
        });
    }

    //
    var space_use_state_list = [];
    jSouper.forEach(appConfig.SPACE_USE_STATE, function(v, k) {
        space_use_state_list.push({
            value: v,
            title: k
        });
    });
    App.set("$Cache.bus.space_use_state_list", space_use_state_list);
    hash_routie.on("orders", function() {
        var queryString = new QueryString(location.hash);
        _num = queryString.get("num") || _num;
        _page = queryString.get("page") || _page;
        _type = queryString.get("type") || _type;
        _load_achs_page(_type, _num, _page);
        App.set("TypeNum", typeNumMap[_type] || "3");
    });
    //页面跳转
    App.set("$Event.bus.achs.changeType", function(e, vm) {

        var data = this.getAttribute("data");
        App.set("TypeNum", data);
        var type = typeMap[data];
        if (type) {
            hash_routie.hash({
                num: _num,
                page: 0,
                type: type
            });
        }
    });

    /*
     * 订单查询
     */
    var _search_ti;
    var _search_xhr;
    App.set("$Event.bus.achs.search", function(e, vm) {
        clearTimeout(_search_ti);
        _search_xhr && _search_xhr.abort();
        _search_xhr = null;
        var ach_key = jSouper.trim(App.get("$Cache.bus.achs.ach_key") || "");
        if (ach_key) {
            App.set("bus.achs_detail", []);
            App.set("$Cache.bus.achs.is_searching", true); //搜索状态
            App.set("$Cache.bus.achs.is_searchend", false); //搜索完成
            _search_ti = setTimeout(function() {
                _search_xhr = coAjax.get(appConfig.bus.find_achs, {
                    ach_key: ach_key,
                    num: 10
                }, function(result) {
                    _search_xhr = null;
                    App.set("bus.achs_detail", result.result);
                    App.set("$Cache.bus.achs.is_searchend", true);
                }, function(errCode, xhr, errorMsg) {
                    alert("error", errorMsg);
                    App.set("$Cache.bus.achs.is_searchend", true);
                });
            }, 200);
        } else {
            App.set("bus.achs_detail", App.get("bus.achs_bak"));
            App.set("$Cache.bus.achs.is_searching", false);
            App.set("$Cache.bus.achs.is_searchend", false);
        }
    });
    /*
     * 订单打印
     */
    App.set("$Event.bus.achs.print", function(e, vm) {
        vm.set("$CPrivate.loding_print", true);
        require(["html2canvas"], function() {
            vm.set("$CPrivate.loding_print", false);
            var element = vm.queryElement({
                className: "order-box"
            })[0];
            if (element) {
                html2canvas(element, {
                    onrendered: function(canvas) {
                        vm.set("info.print_data", canvas.toDataURL());
                    }
                });
            }
        });
    });
    //隐藏订单
    App.set("$Event.bus.achs.hidden_ach", function(e, vm) {
        coAjax.put(appConfig.bus.achs_hidden_ach + vm.get("_id"), function() {
            _load_achs_page(_type, _num, _page);
            alert("success","订单已经放入垃圾箱")
        });
    });
    /*
     * Sock的事件通知
     */
    var bus_notify = App.get("$Cache.bus_notify");
    //订单取消
    bus_notify.on("ach.cancel", function(ach_id) {
        var achs = App.get("bus.achs_detail");
        jSouper.forEach(achs, function(ach) {
            if (ach._id == ach_id) {
                ach.__is_cancel = true;
                App.set("bus.achs_detail", achs);
                return false;
            }
        });
    });
    //新的订单
    bus_notify.on("ach.new", function(ach) {
        var achs = App.get("bus.achs_detail");
        ach.__is_new = true;
        achs.unshift(ach);
        App.set("bus.achs_detail", achs);
    });
    //订单状态改变
    bus_notify.on("ach.state.changed", function(ach_info) {
        var achs = App.get("bus.achs_detail");
        jSouper.forEach(achs, function(ach) {
            if (ach._id == ach_info.ach_id) {
                ach.info.state = ach_info.state;
                App.set("bus.achs_detail", achs);
                return false;
            }
        });
    });
});